package com.ctshk.rpc.finance.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.rpc.finance.base.CustomBaseMapper;
import com.ctshk.rpc.finance.dto.bill.*;
import com.ctshk.rpc.finance.entity.FinanceBill;
import com.ctshk.rpc.finance.req.bill.BillCheckDetailListReq;
import com.ctshk.rpc.finance.req.bill.BillListReq;
import com.ctshk.rpc.finance.req.bill.BillRefundListReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 财务收款及退款单 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-26
 */
public interface FinanceBillMapper extends CustomBaseMapper<FinanceBill> {

    /**
     * 认款列表
     * @param iPage
     * @param req
     * @return
     */
    @Select("<script>SELECT a.*,b.confirm_amount,b.confirm_status,b.confirm_user_id,b.confirm_time,b.confirm_user_name\n" +
            "FROM t_finance_bill a\n" +
            "LEFT JOIN t_finance_bill_confirm b ON a.confirm_id = b.id\n" +
            "WHERE a.bill_type = 1 AND a.is_deleted = 0\n" +
            "<if test=\"req.saleDeptId != null\">AND a.sale_dept_id = #{req.saleDeptId}</if> \n" +
            "<if test=\"req.collectDateBegin != null and req.collectDateEnd != null\">AND a.collection_time BETWEEN #{req.collectDateBegin} AND #{req.collectDateEnd}</if> \n" +
            "<if test=\"req.confirmDateBegin != null and req.confirmDateEnd != null\">AND b.confirm_time BETWEEN #{req.confirmDateBegin} AND #{req.confirmDateEnd}</if> \n" +
            "<if test=\"req.collectionWayId != null\">AND a.collection_way_id = #{req.collectionWayId}</if> \n" +
            "<if test=\"req.saleUserId != null\">AND a.sale_user_id = #{req.saleUserId}</if> \n" +
            "<if test=\"req.productNumber != null and req.productNumber != ''\">AND a.product_number LIKE CONCAT('%',#{req.productNumber},'%') </if> \n" +
            "<if test=\"req.businessType != null\">AND a.business_type = #{req.businessType}</if> \n" +
            "<if test=\"req.collectionTypeId != null\">AND a.collection_type_id = #{req.collectionTypeId}</if> \n" +
            "<if test=\"req.billNumber != null\">AND a.bill_number LIKE CONCAT('%',#{req.billNumber},'%') </if> \n" +
            "<if test=\"req.productName != null\">AND a.product_name LIKE CONCAT('%',#{req.productName},'%') </if> \n" +
            "<if test=\"req.confirmStatus != null and req.confirmStatus != 1 and req.confirmStatus != 4\">AND b.confirm_status = #{req.confirmStatus}</if> \n" +
            "<if test=\"req.confirmStatus != null and req.confirmStatus == 1\">AND a.confirm_id IS NULL</if> \n" +
            "<if test=\"req.confirmStatus != null and req.confirmStatus == 4\">AND a.is_reject = 1</if> \n" +
            "ORDER BY a.collection_time DESC</script>")
    Page<BillListDTO> queryListByCondition(@Param("page") Page<BillListDTO> iPage, @Param("req") BillListReq req);

    /**
     * 根据批量ID统计金额
     */
    @Select("<script>"+"SELECT SUM(collection_amount) \n" +
            "FROM t_finance_bill\n" +
            "WHERE bill_type = 1 AND is_deleted = 0 AND id IN \n"+
            "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "#{id}\n" +
            "</foreach>"+
            "</script>")
    BigDecimal sumCollectionAmountByBatchId(@Param("list") List<Long> idList);

    /**
     * 根据批量ID统计币种
     */
    @Select("<script>"+"SELECT currency_id,currency_name,exchange_rate,SUM(if(bill_type='1',total_amount,NULL)) AS collectAmount,SUM(if(bill_type='2',total_amount,NULL)) AS refundAmount \n" +
            "FROM t_finance_on_account_usage_record\n" +
            "WHERE bill_type = 1 AND is_deleted = 0 AND id IN "+
            "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "#{id}\n" +
            "</foreach>"+
            "</script>")
    List<CheckAmountStaticsDTO> sumCurrencyStaticsByBatchId(@Param("list") List<Long> list);


    /**
     * 批量关联认款单
     * @param id
     * @param userId
     * @param idList
     * @return
     */
    @Update("<script>"+"UPDATE t_finance_bill\n" +
            "SET confirm_id = #{confirm_id},modified_id = #{user_id}\n" +
            "WHERE is_deleted = 0 AND id IN \n"+
            "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "#{id}\n" +
            "</foreach>"+
            "</script>")
    Integer batchUpdateConfirmId(@Param("confirm_id")Long id, @Param("user_id")Long userId, @Param("list")List<Long> idList);

    /**
     * 对账详情-关联认款列表
     * @param iPage
     * @param checkId
     * @return
     */
    @Select("SELECT b.* \n" +
            "FROM t_finance_bill_check_detail a\n" +
            "LEFT JOIN t_finance_bill b ON a.bill_id = b.id\n" +
            "WHERE a.check_id = #{check_id} AND a.is_deleted = 0")
    Page<BillToConfirmListDTO> queryListByCheck(@Param("iPage") Page<BillToConfirmListDTO> iPage, @Param("check_id") Long checkId);

    /**
     * 销售订单退款列表
     * @param iPage
     * @param req
     * @return
     */
    @Select("<script>SELECT a.*\n" +
            "FROM t_finance_bill a\n" +
            "WHERE a.bill_type = 2 AND a.is_deleted = 0\n" +
            "<if test=\"req.saleDeptId != null and req.saleDeptId.size() > 0\">AND a.sale_dept_id IN #{req.saleDeptId}</if> \n" +
            "<if test=\"req.applyTimeBegin != null and req.applyTimeEnd != null\">AND a.refund_time BETWEEN #{req.applyTimeBegin} AND #{req.applyTimeEnd}</if> \n" +
            "<if test=\"req.saleUserName != null\">AND a.sale_user_name LIKE CONCAT('%',#{req.saleUserName},'%')</if> \n" +
            "<if test=\"req.productNumber != null and req.productNumber != ''\">AND a.product_number LIKE CONCAT('%',#{req.productNumber},'%') </if> \n" +
            "<if test=\"req.businessType != null\">AND a.business_type = #{req.businessType}</if> \n" +
            "<if test=\"req.refundTypeId != null\">AND a.refund_type_id = #{req.refundTypeId}</if> \n" +
            "<if test=\"req.billNumber != null\">AND a.bill_number LIKE CONCAT('%',#{req.billNumber},'%') </if> \n" +
            //    "<if test=\"req.productName != null\">AND a.product_name LIKE CONCAT('%',#{req.productName},'%') </if> \n" +
            "<if test=\"req.refundStatus != null\">AND a.refund_status = #{req.refundStatus}</if> \n" +
            "<if test=\"req.resourceInfo != null\">AND a.resource_info LIKE CONCAT('%',#{req.resourceInfo},'%')</if> \n" +
            "ORDER BY a.refund_time DESC</script>")
    Page<BillRefundListDTO> queryRefundListByCondition(@Param("iPage")Page<BillRefundListDTO> iPage, @Param("req")BillRefundListReq req);

    /**
     * 查询认款确认信息
     * @param billNumber
     * @param businessType
     * @return
     */
    @Select("SELECT a.id,a.confirm_id,b.confirm_status \n" +
            "FROM t_finance_bill a \n" +
            "LEFT JOIN t_finance_bill_confirm b ON a.confirm_id = b.id\n" +
            "WHERE a.bill_number = #{bill_number} AND a.business_type = #{business_type} AND b.confirm_status = 4 AND a.is_deleted = 0")
    BillReConfirmDTO queryConfirm(@Param("bill_number") String billNumber, @Param("business_type") Integer businessType);

    /**
     * 往来明细列表
     * @param iPage
     * @param req
     * @return
     */
    @Select("<script>SELECT a.*,a.collection_way_name AS payWay,a.collection_amount AS totalAmount,c.customer_name,c.check_number,c.approval_status \n" +
            "FROM t_finance_bill a\n" +
            "LEFT JOIN t_finance_bill_check_detail b ON a.id = b.bill_id\n" +
            "LEFT JOIN t_finance_bill_check c ON b.check_id = c.id\n" +
            "WHERE a.is_deleted = 0 AND a.tourist_source = 2\n"+
            "<if test=\"req.customerName != null and req.customerName != ''\">AND c.customer_name LIKE CONCAT('%',#{req.customerName},'%') </if> \n" +
            "<if test=\"req.createDeptId != null\">AND a.sale_dept_id = #{req.createDeptId}</if> \n" +
            "<if test=\"req.createDateBegin != null and req.createDateEnd != null\">AND a.gmt_create BETWEEN #{req.createDateBegin} AND #{req.createDateEnd}</if> \n" +
            "<if test=\"req.saleUserId != null\">AND a.sale_user_id = #{req.saleUserId}</if> \n" +
            "<if test=\"req.productNumber != null and req.productNumber != ''\">AND a.product_number LIKE CONCAT('%',#{req.productNumber},'%') </if> \n" +
            "<if test=\"req.businessType != null and req.businessType.size() > 0\">AND a.business_type IN #{req.businessType}</if> \n" +
            "<if test=\"req.billType != null\">AND a.bill_type = #{req.billType}</if> \n" +
            "<if test=\"req.billNumber != null\">AND a.bill_number LIKE CONCAT('%',#{req.billNumber},'%') </if> \n" +
            "<if test=\"req.productName != null\">AND a.product_name LIKE CONCAT('%',#{req.productName},'%') </if> \n" +
            "<if test=\"req.checkStatus != null\">AND a.check_status = #{req.checkStatus}</if> \n" +
            "<if test=\"req.offAccountStatus != null\">AND a.off_account_status = #{req.offAccountStatus}</if> \n" +
            "ORDER BY a.gmt_create DESC "+
            "</script>")
    Page<BillCheckDetailListDTO> selectByCondition(@Param("iPage")Page<BillCheckDetailListDTO> iPage, @Param("req")BillCheckDetailListReq req);
}
